/* $Id: NonClientCompanyRegisterDelegateImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.delegate.provider;

import java.util.ArrayList;
import java.util.List;

import ae.pcfc.etks.online.common.delegate.NonClientCompanyRegisterDelegate;
import ae.pcfc.etks.online.common.exception.AdminWorkflowException;
import ae.pcfc.etks.online.common.exception.SystemException;
import ae.pcfc.etks.online.common.facade.AppMetaDataFacade;
import ae.pcfc.etks.online.common.facade.NonClientCompanyRegisterFacade;
import ae.pcfc.etks.online.common.vo.ClientCategoryVO;
import ae.pcfc.etks.online.common.vo.NonClientCompanyVO;
import ae.pcfc.etks.online.wf.ConstantsWF;
import ae.pcfc.etks.online.wf.service.client.NonClientWF;
import ae.pcfc.etks.online.wf.service.client.provider.NonClientCompanyWFImpl;
import ae.pcfc.etks.online.wf.service.client.vo.CompleteStepVO;
import ae.pcfc.etks.online.wf.service.client.vo.NonClientLaunchIncidentVO;

public class NonClientCompanyRegisterDelegateImpl extends BaseDelegateImpl implements NonClientCompanyRegisterDelegate{
	/*
	 * Here we coordinate between the Facade service which handles DB entities
	 * and the WF both will be looked up with service locator normally we will
	 * have 1-1 relationship between the delegate and the facade and 1-1
	 * relationship between the delegate and the WF service which means for
	 * CompanyRegistrationDelegate there will be ClientRegistrationFacade &
	 * CompanyRegistrationWF validations that related to DB vs WF are maintained
	 * here ie user cannot issue a new request while he already have a request
	 * in the wf Exception are translated in to business exception
	 */
	/**
	 * 
	 */
	public NonClientCompanyRegisterDelegateImpl(){
		
	}
	
	/**
	 * registerNonClientCompany
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.NonClientCompanyRegisterDelegate#registerNonClientCompany(ae.pcfc.etks.online.common.vo.NonClientCompanyVO)
	 * 
	 */
	public String registerNonClientCompany(NonClientCompanyVO nonClientCompanyVO) 
	{		
	    //return serviceLocator.getFacadeService(NonClientCompanyRegisterFacade.class).registerNonClientCompany(nonClientCompanyVO);
	    String clientReferenceNumber = null;
	    NonClientCompanyRegisterFacade nonClientCompanyRegisterFacade = serviceLocator.getFacadeService(NonClientCompanyRegisterFacade.class);
	    AppMetaDataFacade appMetaDataFacade = serviceLocator.getFacadeService(AppMetaDataFacade.class);
	    NonClientWF nonClientWF =serviceLocator.getWorkflowService(NonClientCompanyWFImpl.class);
	    if(nonClientCompanyRegisterFacade.validateNonClientCompanyReq(nonClientCompanyVO)){
        	    String applicationID = appMetaDataFacade.getNextApplicationId();
        	    nonClientCompanyVO.setStrApplicationId(applicationID);
        	    NonClientLaunchIncidentVO launchIncidVO = new NonClientLaunchIncidentVO();
        	    launchIncidVO.setgApplicationID(applicationID);
        	    
        	    nonClientCompanyRegisterFacade.updateWorkflowIncidentVO(launchIncidVO,nonClientCompanyVO.getiClientRole());
        	    Integer wfIncidentId = null;
        	    
        	    try{
            		
        	    	wfIncidentId = nonClientWF.launchNonClientWF(launchIncidVO);        	    
        	    	nonClientCompanyVO.setStrWFIncidentId(wfIncidentId.toString());
        			clientReferenceNumber = nonClientCompanyRegisterFacade.registerNonClientCompany(nonClientCompanyVO);
        	    
        	    }
        	    catch(RuntimeException t)
        	    {
        	    	if(wfIncidentId != null)
        	    	{
	        			CompleteStepVO completeStepVO = new CompleteStepVO();
	        			completeStepVO.setStrUserName(ConstantsWF.ULTIMUS_DEFAULT_INITIATOR_NAME);
	        			completeStepVO.setnIncidentNumber(wfIncidentId);
	        			completeStepVO.setStrSummary(ConstantsWF.ROLLBACK_INCIDENT);
	        			nonClientWF.abortIncident(completeStepVO);
        	    	}        		   	
        			
        			throw new SystemException("Unable to get Ultimus Incident Id or Incident Id is null");        			
        	    }
	    }	
	    return clientReferenceNumber;
	}

	/**
	 * getClientCategory
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.NonClientCompanyRegisterDelegate#getClientCategory(java.lang.String)
	 * 
	 */
	public List<ClientCategoryVO> getClientCategory(String clientType) {
		
		return serviceLocator.getFacadeService(NonClientCompanyRegisterFacade.class).getClientCategory(clientType);
	}

}
